//
//  UIView+HLEToast.m
//  YsToolsManager
//
//  Created by 宋宏康 on 2020/9/26.
//

#import "UIView+HLEToast.h"
#import <Masonry/Masonry.h>
#import "UILabel+YsToolsManager.h"
#import <YYCategories/YYCategories.h>
#import <objc/runtime.h>


static char kAssociatedObjectKey_toast;

@implementation UIView (HLEToast)


#pragma mark - ============== 公开方法 ==============

- (void)hle_makeToast:(NSString *)message
{
    _HLEToastView *toastView = [[_HLEToastView alloc] initWithMessage:message];
    [self hle_showToast:toastView duration:3.0f];
}

- (void)hle_makeToast:(NSString *)message
             duration:(NSTimeInterval)duration
{
    _HLEToastView *toastView = [[_HLEToastView alloc] initWithMessage:message];
    [self hle_showToast:toastView duration:duration];
}

- (void)hle_makeToast:(NSString *)message
             duration:(NSTimeInterval)duration
      backgroundColor:(UIColor *)backgroundColor
{
    _HLEToastView *toastView = [[_HLEToastView alloc] initWithMessage:message];
    toastView.backgroundColor = backgroundColor;
    [self hle_showToast:toastView duration:duration];
}
#pragma mark - ============== 私有方法 ==============

- (void)hle_showToast:(UIView *)toast duration:(NSTimeInterval)duration
{
    [self addSubview:toast];
    
    [toast mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(228);
        make.left.mas_greaterThanOrEqualTo(30);
        make.right.mas_lessThanOrEqualTo(-30);
    }];
    
    //执行显示动画
    toast.alpha = 0.0;
    [UIView animateWithDuration:0.5 animations:^{
        toast.alpha = 1;
    } completion:^(BOOL finished) {
        NSTimer *timer = [NSTimer timerWithTimeInterval:duration target:self selector:@selector(toastTimerDidFinish:) userInfo:toast repeats:NO];
        [NSRunLoop.currentRunLoop addTimer:timer forMode:NSRunLoopCommonModes];
        objc_setAssociatedObject(toast, &kAssociatedObjectKey_toast, timer, OBJC_ASSOCIATION_RETAIN);
    }];
}


- (void)hle_hideToast:(UIView *)toast
{
    [UIView animateWithDuration:0.5 animations:^{
        toast.alpha = 0;
    } completion:^(BOOL finished) {
        [toast removeFromSuperview];
    }];
}

#pragma mark - ============== 事件 ==============

- (void)toastTimerDidFinish:(NSTimer *)timer {
    [self hle_hideToast:(UIView *)timer.userInfo];
    [timer invalidate];
}

@end



@interface _HLEToastView()
@property(nonatomic, strong) UIVisualEffectView *effectView;
/// toast信息
@property(nonatomic, strong) UILabel *messageLabel;
@property(nonatomic, strong) NSString *msg;
@end

@implementation _HLEToastView

#pragma mark - ============== 初始化方法 ==============

- (instancetype)initWithMessage:(NSString *)msg
{
    self = [super init];
    if (self) {
        self.msg = msg;
        [self initUI];
    }
    return self;
    
}

- (void)initUI
{
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    self.layer.cornerRadius = 5.0f;
    self.layer.masksToBounds = YES;
    
    [self addSubview:self.effectView];
    [self.effectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    
    [self addSubview:self.messageLabel];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(15);
        make.bottom.right.mas_equalTo(-15);
    }];
}

#pragma mark - ============== setter/getter ==============
- (UILabel *)messageLabel
{
    if (!_messageLabel) {
        _messageLabel = [UILabel creatLabel:self.msg textColor:[UIColor colorWithHexString:@"#ffffff"] fontSize:[UIFont boldSystemFontOfSize:12] numberofLines:0];
        _messageLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _messageLabel;
}

- (UIVisualEffectView *)effectView
{
    if (!_effectView) {
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        _effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    }
    return _effectView;
}
@end
